/**
 * Created by yumeng on 2017/11/26.
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/example/template/exampleTree.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('CRUD_TYPE,NATIONALITY,PUBLISH_STATUS');

    var parseTime = yufp.util.dateFormat;
    var vm = yufp.custom.vue({
      el: '#exampleTree',
      data: function () {
        var me = this;
        return {
          async: false,
          param: {UNITID: '0000', LEVELUNIT: '1'},
          queryFields: [
            {placeholder: this.$t('exampleTree.bt'), field: 'title', type: 'input'},
            {placeholder: this.$t('exampleTree.lx'), field: 'type', type: 'select', dataCode: 'NATIONALITY' }
          ],
          tableColumns: [
            { label: this.$t('exampleTree.bm'), prop: 'id', width: 80 },
            { label: this.$t('exampleTree.mc'), prop: 'title', width: 250, sortable: true, resizable: true },
            { label: this.$t('exampleTree.lx'), prop: 'type', width: 110, dataCode: 'NATIONALITY' },
            { label: this.$t('exampleTree.yds'), prop: 'pageviews', width: 100 },
            { label: this.$t('exampleTree.zt'), prop: 'status', width: 120, dataCode: 'PUBLISH_STATUS' },
            { label: this.$t('exampleTree.sj'), prop: 'create_at'}
          ],

          updateFields: [{
            columnCount: 2,
            fields: [
              { field: 'title', label: this.$t('exampleTree.mc') },
              { field: 'create_at', label: this.$t('exampleTree.sj'), type: 'date' },
              { field: 'author', label: this.$t('exampleTree.zz') },
              { field: 'auditor', label: this.$t('exampleTree.shr') },
              { field: 'type', label: this.$t('exampleTree.lx'), type: 'select', dataCode: 'NATIONALITY' },
              { field: 'status', label: this.$t('exampleTree.zt'), type: 'select', dataCode: 'PUBLISH_STATUS' }
            ]
          }, {
            columnCount: 1,
            fields: [
              { field: 'remark', label: this.$t('exampleTree.dp'), type: 'textarea', rows: 3 }
            ]
          }],
          updateButtons: [
            {label: this.$t('exampleTree.qx'),
              type: 'primary',
              icon: 'yx-undo2',
              hidden: false,
              click: function (model) {
                me.dialogVisible = false;
              }},
            {label: this.$t('exampleTree.bc'),
              type: 'primary',
              icon: 'check',
              hidden: false,
              click: function (model) {
                me.dialogVisible = false;
                me.$msgbox.alert(model, me.$t('exampleTree.ts'));
              // 请调用服务进行后台保存
              }}
          ],
          height: yufp.frame.size().height,
          dialogVisible: false,
          formDisabled: false,
          viewType: 'DETAIL',
          viewTitle: yufp.lookup.find('CRUD_TYPE', false)
        };
      },
      methods: {
        switchStatus: function (viewType, editable) {
          this.viewType = viewType;
          this.dialogVisible = true;
          this.formDisabled = !editable;
          this.updateButtons[0].hidden = !editable;
          this.updateButtons[1].hidden = !editable;
        },
        nodeClickFn: function (nodeData, node, self) {
          vm.$refs.mytable.remoteData();
        },
        addFn: function () {
          var me = this;
          this.switchStatus('ADD', true);
          this.$nextTick(function () {
            this.$refs.myform.resetFields();
          });
        },
        modifyFn: function () {
          if (this.$refs.mytable.selections.length < 1) {
            this.$message({message: this.$t('exampleTree.qxxzytjl'), type: 'warning'});
            return;
          }
          this.switchStatus('EDIT', true);
          this.$nextTick(function () {
            yufp.extend(this.$refs.myform.formModel, this.$refs.mytable.selections[0]);
          });
        },
        infoFn: function () {
          if (this.$refs.mytable.selections.length < 1) {
            this.$message({message: this.$t('exampleTree.qxxzytjl'), type: 'warning'});
            return;
          }
          this.switchStatus('DETAIL', false);
          this.$nextTick(function () {
            yufp.extend(this.$refs.myform.formModel, this.$refs.mytable.selections[0]);
          });
        }
      }
    });
  };
});
